package com.voiceqsologger.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.sound.sampled.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 音频设备管理服务
 * 提供设备枚举、选择与音频采集功能
 */
@Slf4j
@Service
public class AudioDeviceService {

    /**
     * 获取所有可用的音频输入设备
     */
    public List<AudioDeviceInfo> listInputDevices() {
        List<AudioDeviceInfo> devices = new ArrayList<>();
        
        for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
            try {
                Mixer mixer = AudioSystem.getMixer(mixerInfo);
                Line.Info[] targetLines = mixer.getTargetLineInfo();
                
                for (Line.Info lineInfo : targetLines) {
                    if (TargetDataLine.class.isAssignableFrom(((DataLine.Info) lineInfo).getLineClass())) {
                        AudioDeviceInfo device = new AudioDeviceInfo();
                        device.setName(mixerInfo.getName());
                        device.setDescription(mixerInfo.getDescription());
                        device.setVendor(mixerInfo.getVendor());
                        device.setVersion(mixerInfo.getVersion());
                        devices.add(device);
                        break; // 每个 Mixer 只添加一次
                    }
                }
            } catch (Exception e) {
                log.debug("检查设备失败: {}", mixerInfo.getName(), e);
            }
        }
        
        log.info("发现 {} 个音频输入设备", devices.size());
        return devices;
    }

    /**
     * 根据设备名称打开音频输入流
     */
    public TargetDataLine openInputDevice(String deviceName) throws LineUnavailableException {
        AudioFormat format = new AudioFormat(16000, 16, 1, true, false);
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
        
        for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
            if (mixerInfo.getName().contains(deviceName)) {
                Mixer mixer = AudioSystem.getMixer(mixerInfo);
                TargetDataLine line = (TargetDataLine) mixer.getLine(info);
                line.open(format, 4096);
                line.start();
                log.info("成功打开音频设备: {}", deviceName);
                return line;
            }
        }
        
        throw new LineUnavailableException("未找到匹配的音频输入设备: " + deviceName);
    }

    /**
     * 音频设备信息
     */
    public static class AudioDeviceInfo {
        private String name;
        private String description;
        private String vendor;
        private String version;

        // Getters and Setters
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        
        public String getDescription() { return description; }
        public void setDescription(String description) { this.description = description; }
        
        public String getVendor() { return vendor; }
        public void setVendor(String vendor) { this.vendor = vendor; }
        
        public String getVersion() { return version; }
        public void setVersion(String version) { this.version = version; }
    }
}
